<template>
  <div>
    <el-card class="box-card">
      <h2>编辑用户</h2>
      <el-form :model="userForm" label-width="120px">
        <el-form-item label="用户名：">
          <el-input v-model="userForm.username" readonly></el-input>
        </el-form-item>
        <el-form-item label="新用户名：">
          <el-input v-model="userForm.newUsername"></el-input>
        </el-form-item>
        <el-form-item label="邮箱：">
          <el-input v-model="userForm.email"></el-input>
        </el-form-item>
        <el-form-item label="新密码：">
          <el-input type="password" v-model="userForm.password"></el-input>
        </el-form-item>
        <el-form-item label="确认密码：">
          <el-input type="password" v-model="userForm.confirmPassword"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm">提交</el-button>
          <el-button @click="resetForm('userForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      userForm: {
        username: '',
        newUsername: '',
        email: '',
        password: '',
        confirmPassword: ''
      },
      selectedUser: null
    };
  },
  created() {
    this.selectedUser = this.$route.params.username;  // 获取路由参数中的用户名
    this.fetchUserDetailsByName(this.selectedUser);
  },
  methods: {
    fetchUserDetailsByName(username) {
      this.axios.get(`/api/user/search/${username}`)
          .then(response => {
            const user = response.data.data;
            this.userForm.username = user.username;
            this.userForm.email = user.email;
            this.userForm.password = '';
            this.userForm.confirmPassword = '';
          })
          .catch(error => {
            console.error('Error fetching user details:', error);
          });
    },
    submitForm() {
      if (this.userForm.password !== this.userForm.confirmPassword) {
        this.$message.error('密码和确认密码不一致！');
        return;
      }

      const updatedUser = {
        username: this.userForm.newUsername || this.userForm.username,
        email: this.userForm.email,
        password: this.userForm.password
      };

      this.axios.put(`/api/user/edit/${this.userForm.username}`, updatedUser)
          .then(() => {
            this.$message({
              message: '用户更新成功',
              type: 'success'
            });
            this.$router.push('/list_user');
          })
          .catch(error => {
            console.error('Error updating user:', error);
            this.$message.error('用户更新失败');
          });
    },
    resetForm() {
      this.userForm = {
        username: '',
        newUsername: '',
        email: '',
        password: '',
        confirmPassword: ''
      };
    }
  }
};
</script>

<style scoped>
.box-card {
  margin: auto;
  width: 600px;
}

.el-form-item {
  margin-bottom: 20px;
}

.el-input[readonly] {
  user-select: none;
  pointer-events: none;
  background-color: #f5f5f5;
}
</style>
